<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final
 * @category    Models
 * @see 		Products
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Products_model extends CI_Model {
	public function __construct()
	{
		parent::__construct();
	}

	public function show($uri, $num_rows = false)
	{
		$this->db->query('SET NAMES "LATIN1"');
		if ($uri['limit'] == '') $uri['limit'] = 50;
		$this->db->from('products AS p');
		if ($uri['orderby'] != '') $this->db->where('p.active', 1);
		if ($uri['orderby'] == 'order_new') $this->db->where('p.new', 1);
		if ($uri['orderby'] == 'order_hot') $this->db->where('p.hot', 1);
		if ($uri['orderby'] == 'order_best') $this->db->where('p.best', 1);
		if ($uri['q'] != '')
		{
			$this->db->where(sprintf('(p.name_vn LIKE "%%%s%%" OR p.name_en LIKE "%%%s%%" OR p.keyword LIKE "%%%s%%")', $uri['q'], $uri['q'], str_replace(' ', '-', $uri['q'])));
		}
		if ($uri['cat'] != '')
		{
			$this->db->where(sprintf('(p.cat = "%s" OR p.cat LIKE "%%\"%s\"%%")', $uri['cat'], $uri['cat']));
		}
		$this->db->where('p.deleted', $uri['deleted']);
		if ($num_rows)
		{
			$this->db->select('id');
			return $this->db->get()->num_rows();
		}
		else
		{
			$last_login = $GLOBALS['user']['last_login'];
			$this->db->select('p.*, c.name_vn AS cat, COUNT(m.id) AS comment, IF ('.sprintf('p.date_added >= %1$s', $this->db->escape($last_login)).', 1, 0) as isnew, p1.name_vn AS dvt, (SELECT SUM(IF(type = 0, soluong, 0)) FROM warehouse_inout_details WHERE product = CONCAT("", p.id, "")) AS nhapkho, (SELECT SUM(IF(type = 1, soluong, 0)) FROM warehouse_inout_details WHERE product = CONCAT("", p.id, "")) AS xuatkho, (SELECT SUM(IF(type = 2, soluong, 0)) FROM warehouse_inout_details WHERE product = CONCAT("", p.id, "")) AS trahang, (SELECT SUM(IF(type = 4 AND confirm = 1, soluong, 0)) FROM warehouse_inout_details WHERE product = CONCAT("", p.id, "")) AS huyhang', false);
			$this->db->group_by('id');
			$this->db->join('comments AS m', 'p.id = m.product AND m.reply = 0', 'left');
			$this->db->join('product_properties AS p1', 'p.dvt = p1.id', 'left');
			$this->db->join('product_categories AS c', 'p.cat = c.id', 'left');
			$this->db->limit($uri['limit'], $uri['rowstart']);
			if ($uri['orderby'] == '') $uri['orderby'] = 'id';
			if ($uri['ordermode'] == '') $uri['ordermode'] = 'desc';
			$this->db->order_by($uri['orderby'].' '.$uri['ordermode']);
			$query = $this->db->get();
			if ($query->num_rows() > 0)
			{
				$data = $query->result_array();
				$query->free_result();
				return $data;
			}
			else
			{
				return false;
			}
		}
	}

	public function info($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('id', $id);
		$query = $this->db->get('products');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			$fields = $this->db->field_data('products');
			$default_data = array();
			foreach ($fields as $field)
			{
				$default_data[$field->name] = $field->default;
			}
			return $default_data;
		}
	}

	public function trade_list()
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->select('name_vn, id');
		$this->db->where('deleted', 0);
		$query = $this->db->get('trademarks');
		$data = array('' => 'Chọn ...');
		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $row)
			{
				$data[$row['id']] = $row['name_vn'];
			}
		}
		return $data;
	}

	public function category_list()
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->from('product_categories AS c');
		$this->db->where('c.deleted', 0);
		$this->db->select('c.id, c.parent, c.name_vn, count(s.parent) AS chirld, (SELECT COUNT(id) FROM products WHERE cat = c.id OR cat LIKE CONCAT("%\"", c.id, "\"%")) AS product', false);
		$this->db->order_by('c.sort_order asc');
		$this->db->group_by('id');
		$this->db->join('product_categories as s', 'c.id = s.parent', 'left');
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
		    return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function article_categories()
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->from('article_categories AS c');
		$this->db->where('c.deleted', 0);
		$this->db->select('c.id, c.parent, c.name_vn, count(s.parent) AS chirld');
		$this->db->order_by('c.sort_order asc');
		$this->db->group_by('id');
		$this->db->join('article_categories as s', 'c.id = s.parent', 'left');
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
		    return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function filter_category_list() {
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->from('product_categories AS c');
		$this->db->select('c.id, c.parent, c.name_vn, COUNT(s.parent) AS chirld, (SELECT COUNT(id) FROM products WHERE cat = c.id OR cat LIKE CONCAT("%\"", c.id, "\"%")) AS product', false);
		$this->db->group_by('id');
		$this->db->join('product_categories as s', 'c.id = s.parent', 'left');
		$this->db->order_by('c.sort_order asc');
		$query = $this->db->get();
		$data = array();
		foreach ($query->result_array() as $val)
		{
			if ($val['chirld'] || $val['product']) $data[] = $val;
		}
		return $data;
	}

	public function opt_product_list($json_string)
	{
		$array = json_decode($json_string);
		$data = array();
		if (!count($array))
		{
			return $data;
		}
		$this->db->select('id, name_vn');
		$this->db->where('active', 1);
		$this->db->where('deleted', 0);
		$this->db->where_in('id', $array);
		$query = $this->db->get('products');
		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $row)
			{
				$data[$row['id']] = $row['name_vn'];
			};
		}
		return $data;
	}

	public function properties_data($property)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('cat', $property);
		$this->db->where('active', 1);
		$this->db->where('deleted', 0);
		$this->db->select('id, name_vn');
		$this->db->order_by('sort_order DESC');
		$query = $this->db->get('product_properties');
		if ($query->num_rows() > 0 )
		{
			return $query->result_array();
		}
		else
		{
			return array();
		}
	}

	public function list_product($uri)
	{
		$this->db->query('SET NAMES "LATIN1"');
		if ($uri['q'] != '')
		{
			$this->db->where(sprintf('(name_vn LIKE "%%%s%%" OR name_en LIKE "%%%s%%" OR keyword LIKE "%%%s%%")', $uri['q'], $uri['q'], str_replace(' ', '-', $uri['q'])));
		}
		if (is_array($uri['proids']))
		{
			$this->db->where_in('id', $uri['proids']);
		}
		else
		{
			$uri['proids'] = str_replace('null', '', $uri['proids']);
			$this->db->where('(cat LIKE \'%"'.$uri['cat'].'"%\' OR cat = \''.$uri['cat'].'\' OR id IN (\'0'.str_replace(',', "','", $uri['proids']).'\'))');
		}
		$this->db->where('deleted', 0);
		$this->db->where('active', 1);
		if ($uri['orderby'] == '') $uri['orderby'] = 'id';
		if ($uri['ordermode'] == '') $uri['ordermode'] = 'desc';
		$this->db->order_by($uri['orderby'].' '.$uri['ordermode']);
		if ($uri['limit'])
		{
			if ($uri['rowstart'] == '') $uri['rowstart'] = 0;
			$this->db->limit($uri['limit'], $uri['rowstart']);
		}
		$this->db->select('id, img, name_vn, gia2, code');
		$query = $this->db->get('products');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function num_product($uri)
	{
		if ($uri['q'] != '')
		{
			$this->db->where(sprintf('(name_vn LIKE "%%%s%%" OR name_en LIKE "%%%s%%" OR keyword LIKE "%%%s%%")', $uri['q'], $uri['q'], str_replace(' ', '-', $uri['q'])));
		}
		$this->db->where('(cat LIKE \'%"'.$uri['cat'].'"%\' OR id IN (\'0'.str_replace(',', "','", $uri['proids']).'\'))');
		$this->db->where('deleted', 0);
		$this->db->where('active', 1);
		$this->db->select('id');
		return $this->db->get('products')->num_rows();
	}

	public function list_article($uri)
	{
		$this->db->query('SET NAMES "LATIN1"');
		if ($uri['limit'] == '') $uri['limit'] = 10;
		if ($uri['rowstart'] == '') $uri['rowstart'] = 0;
		if ($uri['q'] != '')
		{
			$this->db->where(sprintf('(name_vn LIKE "%%%s%%" OR name_en LIKE "%%%s%%" OR keyword LIKE "%%%s%%")', $uri['q'], $uri['q'], str_replace(' ', '-', $uri['q'])));
		}
		$uri['newsids'] = str_replace('null', '', $uri['newsids']);
		$this->db->where('(cat = "'.$uri['cat'].'" OR id IN (\'0'.str_replace(',', "','", $uri['newsids']).'\') OR id IN (\'0'.str_replace(',', "','", $uri['articles_added']).'\'))');
		$this->db->where('deleted', 0);
		$this->db->where('active', 1);
		if ($uri['orderby'] == '') $uri['orderby'] = 'id';
		if ($uri['ordermode'] == '') $uri['ordermode'] = 'desc';
		$this->db->order_by($uri['orderby'].' '.$uri['ordermode']);
		$this->db->limit($uri['limit'], $uri['rowstart']);
		$this->db->select('id, img, name_vn');
		$query = $this->db->get('articles');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function num_article($uri)
	{
		if ($uri['q'] != '')
		{
			$this->db->where(sprintf('(name_vn LIKE "%%%s%%" OR name_en LIKE "%%%s%%" OR keyword LIKE "%%%s%%")', $uri['q'], $uri['q'], str_replace(' ', '-', $uri['q'])));
		}
		$this->db->where('(cat = "'.$uri['cat'].'" OR id IN (\'0'.str_replace(',', "','", $uri['newsids']).'\') OR id IN (\'0'.str_replace(',', "','", $uri['articles_added']).'\'))');		
		$this->db->where('deleted', 0);
		$this->db->where('active', 1);
		$this->db->select('id');
		return $this->db->get('articles')->num_rows();
	}

	public function process($data, $id)
	{
		if (!is_array($data))
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');	
		if ($id > 0)
		{
			unset($data['date_added']);
			$this->db->where('id', $id);
			$this->db->update('products', $data);
			write_log('update_products', $data['name_vn']);
		}
		else
		{
			$this->db->insert('products', $data);
			write_log('add_products', $data['name_vn']);
		}
		return true;
	}

	public function quick_save($data, $id)
	{
		if (!is_array($data) || !$id)
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('id', $id);
		$this->db->update('products', $data);
		return true;
	}

}

/* End of file products_model.php */
/* Location: ./application/models/products_model.php */